//
//  RootViewController.m
//  Day_9 UITableView
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RootViewController.h"
#import "FirstViewController.h"
#import "Singleton.h"
#import "Student.h"
@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [Singleton shareSingleton];
    UITableView *myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    //UITableViewStylePlain换行布局样式
    myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myTableView];
    [myTableView release];
    

    myTableView.delegate = self;      //基本配置代理
    myTableView.dataSource = self;    //数据源代理

    //去掉cell的线
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置tableview的表头视图
    UIView *hv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
    hv.backgroundColor = [UIColor cyanColor];
    myTableView.tableHeaderView =hv;
    [hv release];
    
    //刷新整个TableView
    [myTableView reloadData];
    
    //刷新指定的cell
//    myTableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>

}

//设置section个数  区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [Singleton shareSingleton].studentDic.allKeys.count;
}


//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //根据section区的下标取出key
    NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:section];
    //根据key取出数组
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    //当前行数由数组个数决定
    return array.count;
    
}

//设置cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.先从重用池里取
    //queue:队列   reusable:重用   identifier:标识符
    //标识符是区别不同cell的唯一标记
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        //如果cell为空  需要创建cell
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }
    
    NSString *str = [NSString stringWithFormat:@"secetion = %ld  row = %ld", indexPath.section, indexPath.row];
    cell.textLabel.text = str;
    
    if (indexPath.section == 1 && indexPath.row == 2) {
                cell.textLabel.text = @"大水杯";
    }
    
    
    //取消cell点击选中效果
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    
//    if (indexPath.section == 0) {
//        cell.textLabel.text = @"大水杯";
//
//    }
//    if (indexPath.section == 1) {
//        cell.textLabel.text = @"大水表";
//        
//    }
//    if (indexPath.section == 2) {
//        cell.textLabel.text = @"大伞兵";
//        
//    }
    
    NSString *key = [[Singleton shareSingleton].studentDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    Student *stu = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = stu.name;
    
    
    return cell;
}

//设置字母检索
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return [NSArray arrayWithObjects:@"A",@"B", @"C", @"D",   nil];

}

//设置section表头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"A";

    }
    if (section == 1) {
        return @"B";
        
    }
    if (section == 2) {
        return @"C";
        
    }
    return 0;

}

//选中cell触发的方法
//didDeselectRowIndexPath选中时不触发,选中下一个cell时触发
//didSelectRowIndexPath  选中cell时触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstViewController *firstVC = [[FirstViewController alloc]init];
//    [self.navigationController pushViewController:firstVC animated:YES];
    firstVC.indexPath = indexPath;
    NSLog(@"secetion = %ld  row = %ld", indexPath.section, indexPath.row);
    
    //选中有置灰效果  离开自动取消
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //滚动到某一行cell
    //自定义cell下标位置路径
    NSIndexPath *inP = [NSIndexPath indexPathForRow:2 inSection:2];
    [tableView scrollToRowAtIndexPath:inP atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
